package tk.mybatis.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import tk.mybatis.springboot.cons.ResponseConst;

/**
 * Created by NightDream on 2017/9/21.
 */
public class BaseController {
    protected static final String RESPONSE_CODE = "code";
    protected static final String RESPONSE_MESSAGE = "msg";
    /**
     * 返回成功的JsonObject对象
     *
     * @param obj
     * @return
     * @throws Exception
     */
    public JSONObject getSuccessResponse(JSONObject obj) throws Exception {
        JSONObject resultObj = new JSONObject();
        resultObj.put(RESPONSE_CODE, ResponseConst.RESPONSE_SUCCESS);
        resultObj.put(RESPONSE_MESSAGE, "请求成功");
        resultObj.putAll(obj);
        return resultObj;
    }

    /**
     * 返回成功的JsonObject对象,并包含自定义消息
     *
     * @param obj
     * @param customMsg
     * @return
     * @throws Exception
     */
    public JSONObject getSuccessResponse(JSONObject obj, String customMsg) throws Exception {
        JSONObject resultObj = new JSONObject();
        resultObj.put(RESPONSE_CODE, ResponseConst.RESPONSE_SUCCESS);
        resultObj.put(RESPONSE_MESSAGE, customMsg);
        resultObj.putAll(obj);
        return resultObj;
    }

    /**
     * 返回失败的JsonObject对象
     *
     * @param obj
     * @return
     * @throws Exception
     */
    public JSONObject getFaildResponse(JSONObject obj) throws Exception {
        JSONObject resultObj = new JSONObject();
        resultObj.put(RESPONSE_CODE, ResponseConst.RESPONSE_FAILD);
        resultObj.put(RESPONSE_MESSAGE, "请求失败");
        resultObj.putAll(obj);
        return resultObj;
    }

    /**
     * 返回失败的JsonObject对象，并包含自定义错误消息
     *
     * @param obj
     * @param errorMsg
     * @return
     * @throws Exception
     */
    public JSONObject getFaildResponse(JSONObject obj, String errorMsg) throws Exception {
        JSONObject resultObj = new JSONObject();
        resultObj.put(RESPONSE_CODE, ResponseConst.RESPONSE_FAILD);
        resultObj.put(RESPONSE_MESSAGE, errorMsg);
        resultObj.putAll(obj);
        return resultObj;
    }
}
